-- UIRelicFix
-- Created by cheny3
-- 修复遗迹提示

UIRelicFix = class("UIRelicFix", function()
    return cc.Layer:create();
end);

function UIRelicFix.create()
    return UIRelicFix.new();
end

local MACHINE_CLASS_ID = 7282;    -- 采掘核心装置-QC

-- 构造函数
function UIRelicFix:ctor()
    self:setName("UIRelicFix");
    local node = cc.CSLoader:createNode("layout/common/BonusNotify2.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);

    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIRelicFix 界面析构清理");
        end
    end)

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 重绘
function UIRelicFix:redraw()
    -- 设置标题
    local titleLabel = findChildByName(self.node, "CT/title");
    TextStyleM.setTitleStyle(titleLabel);
    titleLabel:setString(getLocStr("fix_relic_title"));

    -- 正文样式
    local descLabel = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);

    -- 详细描述
    local detailLabel = findChildByName(self.node, "CT/desc2");
    TextStyleM.setTextStyle(detailLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);

    -- 确认按钮
    local okBtn = findChildByName(self.node, "CT/btn_ok");
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local BT = findChildByName(self.node, "BT");
    BT:setVisible(true);
    btnBack:setVisible(false);
    okBtn:setTitleText(getLocStr("btn_text_leave"));

    -- 修复需要的物品
    local costArr = FormulaM.invoke("CALC_FIX_RELIC_COST");
    local iconPath = getItemIconPath(ItemM.query(MACHINE_CLASS_ID, "icon"));
    local itemName = ItemM.query(MACHINE_CLASS_ID, "name");

    local item = cc.CSLoader:createNode("layout/equip/equip_item.csb");
    local icon = findChildByName(item, "btn");
    icon:loadTexture(iconPath);
    -- 描述
    descLabel = applyString(descLabel, string.format(getLocStr("fix_relic_tip"), itemName));

    local itemPanel = findChildByName(self.node, "CT/bonus_panel");
    itemPanel:setVisible(true);
    itemPanel:addChild(item);
    item:setPosition(cc.p(250, 50))

    -- 额外描述
    local detail;
    if ME.user.dbase:query("features", "machine", false) then
        -- 完成迷宫内指引/告知金钱
        for _, cost in pairs(costArr) do
            if cost[1] == 2 then
                local attribName = FieldsM.getFieldName(cost[2]);
                local count = cost[3];
                local color = "blue";

                if ME.user:queryAttrib(cost[2]) < cost[3] then
                    -- 金币不够
                    color = "red";
                    okBtn:setTouchEnabled(false);
                    okBtn:setBright(false);
                else
                    okBtn:setTouchEnabled(true);
                    okBtn:setBright(true);
                end
                detail = string.format(getLocStr("fix_relic_tip3"), color, cost[3], attribName);
                break;
            end
        end
        okBtn:setTitleText(getLocStr("btn_text_fix"));
        btnBack:setVisible(true);
    else
        -- 没有完成
        detail = getLocStr("fix_relic_tip2");
    end
    -- 详细描述
    detailLabel = applyString(detailLabel, detail);
    detailLabel:setVisible(true);

    local bg = findChildByName(self.node, "CT/bg_frame");
    -- 自适应
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_SMALL,
        {titleLabel, descLabel, itemPanel, detailLabel, okBtn}, {});

    -- 根据适配后的界面高度比例，对界面上的角标位置进行修正
    local bg1 = findChildByName(self.node, "CT/sub_bg_l");
    local bg2 = findChildByName(self.node, "CT/sub_bg_r");

    -- 这里要注意一下，原始的模版高度并不是100%，而是85%。。不解
    local fixY, deltaY = math.modf(AlignM.BG_SIZE_SMALL.height * (0.85 - bg:getScaleY()) / 2);

    if fixY > 0 then
        fixY = fixY + 9;
    else
        fixY = fixY + 10;
    end

    bg1:setPositionY(bg1:getPositionY() - fixY);
    bg2:setPositionY(bg2:getPositionY() - fixY);
end

-- 检查能否修复
function UIRelicFix:canFixRelic()
    if not ME.user.dbase:query("features", "machine", false) then
        return "no_machine";
    end

    -- 修复需要的物品
    local costArr = FormulaM.invoke("CALC_FIX_RELIC_COST");
    for _, cost in pairs(costArr) do
        if cost[1] == 1 then
            -- 判断道具
            if ItemM.getAmount(ME.user, cost[2]) < cost[3] then
                return "no_item";
            end
        elseif cost[1] == 2 then
            -- 判断金钱
            if ME.user:queryAttrib(cost[2]) < cost[3] then
                return "no_money";
            end
        end
    end
    return true;
end

-- 注册点击事件
function UIRelicFix:registerTouchEvent()
    -- 确认按钮
    local okBtn = findChildByName(self.node, "CT/btn_ok");
    TextStyleM.setTextStyle(okBtn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    okBtn:setTitleText(getLocStr("confirm"));

    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            local ret = self:canFixRelic();
            if type(ret) == "string" then
                if ret == "no_machine" then
                    -- 没获得QC装置/直接关掉界面
                    UIMgr.getCurrentScene():removeFormByName("UIRelicFix");
                end
            elseif true == ret then
                -- 请求开启遗迹
                Operation.cmd_open_relic();
                UIMgr.getCurrentScene():removeFormByName("UIRelicFix");
            end
        end
    end
    okBtn:addTouchEventListener(onOkClick);

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIRelicFix");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
end

-- 适配
function UIRelicFix:resize()
    local node = self.node;

    -- bg充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

